package cn.itcast.config;

import cn.itcast.constants.Constant;
import cn.itcast.tools.DataTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author GY
 * @version V1.0
 * @date 2025-04-20 20:37
 */
@Configuration
public class SpringAIConfig {

    /**
     * ChatClient - ChatClient与大模型交互
     */
    @Bean
    public ChatClient chatClient(ChatClient.Builder builder ,
                                 Advisor simpleLoggerAdvisor,
                                 Advisor messageChatMemoryAdvisor,
                                 DataTools dataTools
    ) {
        return builder
                .defaultSystem(Constant.SYSTEM_ROLE)
                .defaultSystem(Constant.PLANS_DATA)
                .defaultAdvisors(simpleLoggerAdvisor, messageChatMemoryAdvisor)
                .defaultTools(dataTools)
                .build();
    }

    @Bean
    public Advisor simpleLoggerAdvisor() {
        return new SimpleLoggerAdvisor();
    }

    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory) {
        return new MessageChatMemoryAdvisor(chatMemory);
    }

    /**
     * VectorStore -内存向量数据库
     */
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }

}
